﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;
using System.Collections.Generic;
using System.IO;
using Telerik.Web.UI.GridExcelBuilder;
using Tom;

public partial class UserControls_Profile : System.Web.UI.UserControl
{
    private static DataTable dtStatus;
    private static int iPage = 1;
    private static int[] iStatusID = new int[6];
    private static int UserID = -1;
    private static int PageSize = 6;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Session["UserID"] = 1;
            String StrUser = Request.QueryString["UserID"];
            if (String.IsNullOrEmpty(StrUser))
            {
                Response.Redirect("Default.aspx");
            }
            if (!int.TryParse(StrUser, out UserID))
            {
                Response.Redirect("Default.aspx");
            }

            if (UserID == (int)Session["UserID"])
            {
                Response.Redirect("Home.aspx");
            }
            String StrPage = Request.QueryString["Page"];
            if (String.IsNullOrEmpty(StrPage))
            {
                iPage = 1;
            }
            if (!int.TryParse(StrPage, out iPage))
            {
                iPage = 1;
            }

            GetData();
        }
    }
    protected void Page_Prerender(object sender, EventArgs e)
    {
        bool isFriend = Tom.Utils.CheckIsFriend(UserID, (int)Session["UserID"]);
        if (!isFriend)
        {
            for (int i = 1; i <= PageSize; ++i)
            {
                ((TextBox)FindControl("TextBox_Comment" + i.ToString())).Visible = false;
                ((Button)FindControl("Button_Comment" + i.ToString())).Visible = false;
            }
        }

        bool hasNext = false;
        int iCountStatus = PageSize;
        if (dtStatus.Rows.Count <= iPage * PageSize)
        {
            iCountStatus = dtStatus.Rows.Count - ((iPage - 1) * PageSize);
            hasNext = false;
        }
        else
        {
            hasNext = true;
        }

        Panel7.Visible = true;
        HyperLink_NextPage.NavigateUrl = "../Profile.aspx?UserID=" + UserID.ToString() + "Page=" + (iPage + 1).ToString();

        String ControlName;
        for (int i = 0; i < iCountStatus; ++i)
        {
            iStatusID[i] = (int)dtStatus.Rows[(iPage - 1) * PageSize + i]["status_id"];
            ControlName = "Literal" + (i + 1).ToString();
            Literal LiteralContent = (Literal)FindControl(ControlName);
            Panel PanelContent = (Panel)FindControl("Panel" + (i + 1).ToString());
            LiteralContent.Text = LoadData(iStatusID[i]);
            PanelContent.Visible = true;
        }

        if (!hasNext)
        {
            for (int i = iCountStatus; i < PageSize; ++i)
            {
                iStatusID[i] = -1;
                Literal LiteralContent = (Literal)FindControl("Literal" + (i + 1).ToString());
                Panel PanelContent = (Panel)FindControl("Panel" + (i + 1).ToString());
                LiteralContent.Text = "";
                PanelContent.Visible = false;
            }
            Panel7.Visible = false;
        }
    }

    protected void GetData()
    {
        dtStatus = Status.GetAllStatus(UserID);
        DataView dv = dtStatus.DefaultView;
        dv.Sort = "created_date DESC";
        dtStatus = dv.ToTable();
    }

    protected String LoadData(int StatusID)
    {
        String StrContent = "";
        String StrComment = "";
        String StrCommentItem = "";
        Status status = Status.GetInfo(StatusID);
        StrContent = File.ReadAllText(Server.MapPath("~/UserControls/Template/Comment.htm"));
        StrCommentItem = File.ReadAllText(Server.MapPath("~/UserControls/Template/CommentItem.htm"));
        StrContent = StrContent.Replace("$UserName", User.GetInfo(status.UserID).UserName);
        StrContent = StrContent.Replace("$Status", status.Content);
        StrContent = StrContent.Replace("$CreateDate", String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                status.CreateDate.Hour, status.CreateDate.Minute, status.CreateDate.Second,
                status.CreateDate.Day, status.CreateDate.Month, status.CreateDate.Year));
        StrContent = StrContent.Replace("$Profile", "Profile.aspx?UserID=" + status.UserID);
        if (!String.IsNullOrEmpty(User.GetInfo(status.UserID).Avatar.Trim()))
        {
            StrContent = StrContent.Replace("$Avatar", "Files/Avatar" + User.GetInfo(status.UserID).Avatar.Trim());
        }
        else
        {
            StrContent = StrContent.Replace("$Avatar", "Files/Avatars/default.jpg");
        }

        DataTable dtComment = CommentStatus.GetAll(StatusID);
        DataView dv = dtComment.DefaultView;
        dv.Sort = "created_date ASC";
        dtComment = dv.ToTable();
        foreach (DataRow dtRow in dtComment.Rows)
        {
            User friend = User.GetInfo(Friend.GetInfo((int)dtRow["friend_id"]).Friend_user_id);
            String StrTemp = StrCommentItem;
            StrTemp = StrTemp.Replace("$UserName", friend.UserName);

            StrTemp = StrTemp.Replace("$Comment", dtRow["text"].ToString());
            DateTime time = (DateTime)dtRow["created_date"];
            StrTemp = StrTemp.Replace("$CreateDate", String.Format("{0,2:00}:{1,2:00}:{2,2:00} - {3,2:00}/{4,2:00}/{5,4}",
                time.Hour, time.Minute, time.Second,
                time.Day, time.Month, time.Year));
            StrTemp = StrTemp.Replace("$Profile", "Profile.aspx?UserID=" + friend.UserID);
            if (!String.IsNullOrEmpty(friend.Avatar))
            {
                StrTemp = StrTemp.Replace("$Avatar", "Files/Avatar" + friend.Avatar);
            }
            else
            {
                StrTemp = StrTemp.Replace("$Avatar", "Files/Avatars/default.jpg");
            }

            StrComment += StrTemp;
        }

        StrContent = StrContent.Replace("$Comment", StrComment);
        return StrContent;
    }
    protected void Button_Comment1_Click(object sender, EventArgs e)
    {
        Comment(1, TextBox_Comment1.Text.Trim());
    }
    protected void Button_Comment2_Click(object sender, EventArgs e)
    {
        Comment(2, TextBox_Comment2.Text.Trim());
    }
    protected void Button_Comment3_Click(object sender, EventArgs e)
    {
        Comment(3, TextBox_Comment3.Text.Trim());
    }
    protected void Button_Comment4_Click(object sender, EventArgs e)
    {
        Comment(4, TextBox_Comment4.Text.Trim());
    }
    protected void Button_Comment5_Click(object sender, EventArgs e)
    {
        Comment(5, TextBox_Comment5.Text.Trim());
    }
    protected void Button_Comment6_Click(object sender, EventArgs e)
    {
        Comment(6, TextBox_Comment6.Text.Trim());
    }

    protected void Comment(int StatusIndex, string ContentComment)
    {
        if (ContentComment.Length > 0)
        {
            CommentStatus comment = new CommentStatus();
            comment.StatusID = iStatusID[StatusIndex - 1];
            Status status = Status.GetInfo(comment.StatusID);
            int FriendListID = FriendList.GetFrienListID(UserID);
            int FriendID = Friend.GetFriendID(FriendListID, (int)Session["UserID"]);
            comment.FriendID = FriendID;
            comment.Content = ContentComment;
            comment.CreateDate = DateTime.Now;
            CommentStatus.Insert(comment);
            ((Literal)FindControl("Literal" + StatusIndex.ToString())).Text = LoadData(iStatusID[StatusIndex - 1]);
        }
    }
}
